//
//  FileReadAndWrite.m
//  bettaTestFile
//
//  Created by jing zhao on 3/11/15.
//  Copyright (c) 2015 jing zhao. All rights reserved.
//

#import "FileReadAndWrite.h"
#define APPNAME @"bracelet"

@implementation FileReadAndWrite

+(void)storageDataWithFile:(NSString*)fileName andData:(NSDictionary *)dataDic
{
    NSString *strLibPah = [[NSHomeDirectory() stringByAppendingPathComponent:@"Library"]stringByAppendingPathComponent:APPNAME];
    NSString *strStoreFilePath =[NSString stringWithFormat:@"%@/%@",strLibPah,fileName];
    
    NSData *data =[NSKeyedArchiver  archivedDataWithRootObject:dataDic];
     NSError *error;
    if (![[NSFileManager defaultManager]fileExistsAtPath:strStoreFilePath]) {
        if ( ![[NSFileManager defaultManager]fileExistsAtPath:strLibPah]) {
            [[NSFileManager defaultManager]createDirectoryAtPath:strLibPah withIntermediateDirectories:YES attributes:nil error:&error];
        }
        [[NSFileManager defaultManager]createDirectoryAtPath:strLibPah withIntermediateDirectories:YES attributes:nil error:&error];
        [[NSFileManager defaultManager]createFileAtPath:strStoreFilePath contents:nil attributes:nil];
    }
    [data writeToFile:strStoreFilePath options:NSDataWritingAtomic error:&error];
    NSLog(@"%@",error);
    
}


+(NSDictionary*)getTheStorageDataWithFile:(NSString*)fileName
{
    NSString *strLibPah = [[NSHomeDirectory() stringByAppendingPathComponent:@"Library"]stringByAppendingPathComponent:APPNAME];
    NSString *strStorePath =[NSString stringWithFormat:@"%@/%@",strLibPah,fileName];
    if ([[NSFileManager defaultManager]fileExistsAtPath:strStorePath]) {
        NSData *data = [NSData dataWithContentsOfFile:strStorePath];
        NSDictionary *dicTemp = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        return dicTemp;
    }
    return nil;
}

@end
